classdef Helper< handle
    properties (Constant)
        fThresh = 0.10;
    end
    methods(Static = true)
        %% 判断矩形box是否全在 某个矩形区域rect内
        function bIn = IsInRect(box, rect)
            vUp   = [rect(3), rect(3), rect(4), rect(4)]';
            vDown = [rect(1), rect(1), rect(2), rect(2)]';
            
            ins1 = box<= vUp;
            ins2 = box>= vDown;
            
            bIn = all(ins1 & ins2);
        end
        
        function bIn = IsPtInRect(pp, rect)
            vUp   = [rect(3); rect(4)];
            vDown = [rect(1); rect(2)];
            ins1 = pp <= vUp;
            ins2 = pp >= vDown;
            
            bIn = all(ins1 & ins2);
        end
        
        %% box = [xl, xr, yl, yr]
        %% rect = [xl, yl, xr, yr] 两者坐标不对
        function [bIn, inBox, ratio] = IsBoxMostInRect(box, rect)
            ins = [1, 3, 2, 4];
            rect = rect(ins);
            
            ratio = 0;
            inBox = box;
            inBox(1:2:3) = max(box(1:2:3), rect(1:2:3));
            inBox(2:2:4) = min(box(2:2:4), rect(2:2:4));
            
            area1 = Helper.CalcArea(box);
            area2 = Helper.CalcArea(inBox);
            if area2 < 0
                bIn = false;
                return;
            end
            
            ratio = area2/area1;
            if ratio > Helper.fThresh
                bIn = true;
            else
                disp('1111')%%表示box超出rect，不需要保存
                bIn = false;
            end
        end
        
        function area = CalcArea(box)
            ww = box(2) - box(1);
            hh = box(4) - box(3);
            
            flag = ww>0 & hh>0;
            area = ww * hh;
            if flag == false
                area = abs(area) * -1;
            end
        end
        
        function img_sub = CropImage(img, rect, ww)
            szImage = size(img);
            imgWidth = szImage(2);
            imgHeight = szImage(1);
            
            leftX = rect(1);
            topY = rect(2);
            rightX = rect(3);
            bottomY = rect(4);
            
            if rightX < imgWidth && bottomY < imgHeight
                img_sub = imcrop(img, [rect(1)+1, rect(2)+1, ww-1, ww-1]);%%matlab坐标+1
                return
            end
            
            img_sub = ones(ww, ww, 3, 'like', img) * 200;
            
            wwww = ww;
            hhhh = ww;
            if rightX >= imgWidth
                wwww = imgWidth  - rect(1);
            end
            if bottomY >= imgHeight
                hhhh = imgHeight - rect(2);
            end
            
            imgTemp = imcrop(img, [rect(1)+1, rect(2)+1, wwww-1, hhhh-1]);
            img_sub(1:hhhh, 1:wwww, :) = imgTemp;
            
        end
        
        % % % % % % % % % % % % % % % % %
        % % % % 处理学生的标注
        function DoStudentProcess()
            fileName = 'F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\1\93.json'
            %             fileName = 'F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\1\95-2.json'
            fileName = 'F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\1\96.json'
            fileName = 'F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\1\aa.json'
            fileName = 'F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\1\97.json'
            [filepath, name, ext] = fileparts(fileName);
            
            json = jsondecode(fileread(fileName));
            
            imageFile = fullfile(filepath, json.image);
            annotations = json.annotations;
            
            
            img = imread(imageFile);
            [hh, ww, ~] = size(img);
            
            
            template_file =  'F:\玉溪烟草\matlab\template.json';
            
            
            json_temp = JsonObject(template_file);
            json_obj = copy(json_temp);
            json_obj.json_data.imageHeight = hh;
            json_obj.json_data.imageWidth  = ww;
            
            %             shapes =
            for i = 1 : length(annotations)
                obj = annotations(i);
                
                label = obj.label;
                cc = obj.coordinates;
                
                shape.label = label;
                
                cc.x = cc.x - cc.width/2;%% - 20.5;
                cc.y = cc.y - cc.height/2;%% - 58;
                
                
                shape.points = [ [cc.x, cc.y]; [cc.x + cc.width, cc.y + cc.height]];
                
                shape.group = 'null';
                shape.shape_type = 'rectangle';
                shape.flags = struct();
                shapes(i) = shape;
            end
            json_obj.json_data.shapes = shapes;
            %%json_obj.setAttachedData(rect, overlap);%%增加自定义数据
            
            
            ImgName = ['__', name,  '.jpg'];
            ImgName = fullfile(filepath, ImgName);
            json_obj.encodeImageData(img, ImgName, false);
            %             json_obj.encodeImageData(img, ImgName, true);
            
            JsonName = fullfile('F:\玉溪烟草\2022-04-28新微信图片\seed最新标注\2', [name,'.json']);
            json_obj.saveJsonFile(JsonName);
        end
        
        %%增加文件夹
        function files = appendDirs(datapath, files)
            for i = 1 : length(files)
                files{i} = fullfile(datapath, [files{i}, '.json']);
                %                 files{i} = fullfile('..\seed\', [files{i}, '.json']);
            end
        end
        
        % % % % % % % % % % % % % % %
        %%最新画图
        function SaveFigure2Img(imgname)
            %% 获取figure界面
            frame = getframe(gcf);
            %% 转为图像
            im = frame2im(frame);
            %% 保存
            imwrite(im,imgname);
        end
        %%在图像矩阵中画 红框
        function img = Drawrectangle(img, xbounds, ybounds, color)
            img(ybounds, xbounds(1):xbounds(2), 1) = color(1);
            img(ybounds(1):ybounds(2), xbounds, 1) = color(1);
            
            img(ybounds, xbounds(1):xbounds(2), 2) = color(2);
            img(ybounds(1):ybounds(2), xbounds, 2) = color(2);
            
            img(ybounds, xbounds(1):xbounds(2), 3) = color(3);
            img(ybounds(1):ybounds(2), xbounds, 3) = color(3);
        end
    end
end